#!/usr/bin/python

""" tallyTshirtSizes.py
    Pulls info from the DB on the T-shirt size for each user
    Compiles aggregate statistics on this data, as well

    Sends info to the designated e-mail address """

__author__ = "Dylan J. Sather"
__created__ = "2009-07-31"
__lastEdited__ = "2009-07-31"

import smtplib
import MySQLdb

# Connect to the DB and set cursor; fetch result set
db = MySQLdb.connect(host="localhost", user="user", passwd="passwd", db="db")
cursor = tcdb.cursor()

cursor.execute("SELECT shirtsize, first_name, last_name \
		FROM users \
		INNER JOIN users_data \
		ON users.id = users_data.user_id \
		ORDER BY last_name")

msg = ''

for data in cursor.fetchall():
    msg += data[1] + " " + data[2] + " - " + data[0] + "\n"

# Send message
fromAddress = "tshirt.info@grinnell.edu"
toAddress = "tc@grinnell.edu"
subject = "T-shirt sizes"

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (fromAddress, toAddress, subject, msg)
server = smtplib.SMTP('localhost')
server.sendmail(fromAddress, toAddress, message)
server.quit()

# Close up
cursor.close()
tcdb.close()

